import {Component, OnInit} from '@angular/core';
import {OperateService} from '../operate.service';
import {NzMessageService} from 'ng-zorro-antd/message';

enum GROUPTYPE {
  '初级' = 1,
  '中级',
  '高级',
}

enum TYPE {
  '积分' = 1,
  '点豆',
  '实物',
  '谢谢惠顾',
}

@Component({
  selector: 'app-lottery',
  templateUrl: './lottery.component.html',
  styleUrls: ['./lottery.component.scss']
})
export class LotteryComponent implements OnInit {
  firstGamePrizeList = [];
  middleGamePrizeList = [];
  highGamePrizeList = [];

  editCommonModal = {
    visible: false,
    id: '',
    name: '',
    shortName: '',
    img: '',
    type: '',
    groupType: '',
    stock: {status: false, num: ''},
    limitIntegral: {type: 0, num: ''},
    value: '',
    probability: '',
  };
  editThanksModal = {
    visible: false,
    id: '',
    groupType: '',
    img: '',
  };
  GROUPTYPE = GROUPTYPE;
  TYPE = TYPE;

  constructor(
    private operateService: OperateService,
    private nzMessageService: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.getLottery();
  }

  getLottery() {
    this.operateService.getLottery().then((res: any) => {
      if (res.code === 0) {
        console.log(res);
        this.firstGamePrizeList = res.data.firstGamePrizeList;
        this.middleGamePrizeList = res.data.middleGamePrizeList;
        this.highGamePrizeList = res.data.highGamePrizeList;
      }
    });
  }

  showModal(data: any) {
    if (data.type === 4) {
      this.editThanksModal = {
        visible: true,
        id: data.id,
        groupType: data.groupType,
        img: data.img,
      };
    } else {
      this.editCommonModal = {
        visible: true,
        id: data.id,
        name: data.name,
        shortName: data.shortName,
        img: data.img,
        type: data.type,
        groupType: data.groupType,
        stock: data.stock === 0 ? {status: true, num: ''} : {status: false, num: data.stock},
        limitIntegral: {type: data.limitType, num: data.limitIntegral},
        value: data.value,
        probability: data.probability,
      };
    }
  }

  hideModal() {
    this.editCommonModal = {
      visible: false,
      id: '',
      name: '',
      shortName: '',
      img: '',
      type: '',
      groupType: '',
      stock: {status: false, num: ''},
      limitIntegral: {type: 0, num: ''},
      value: '',
      probability: '',
    };
    this.editThanksModal = {
      visible: false,
      id: '',
      groupType: '',
      img: '',
    };
  }

  saveEditModal() {
    if (!this.editCommonModal.name) {
      this.nzMessageService.error('请输入奖品名称');
      return;
    }
    if (!this.editCommonModal.shortName) {
      this.nzMessageService.error('请输入奖品简称');
      return;
    }
    if (!this.editCommonModal.img) {
      this.nzMessageService.error('请选择图片');
      return;
    }
    if (!this.editCommonModal.type) {
      this.nzMessageService.error('请选择奖品类型');
      return;
    }
    if (!this.editCommonModal.stock.status && (this.editCommonModal.stock.num === '' || this.editCommonModal.stock.status === null)) {
      this.nzMessageService.error('请输入奖品数量');
      return;
    }
    if (this.editCommonModal.limitIntegral.type !== 0 && this.editCommonModal.limitIntegral.num === '') {
      this.nzMessageService.error('请选择解锁条件');
      return;
    }
    if (!this.editCommonModal.value) {
      this.nzMessageService.error('请输入奖品价值');
      return;
    }
    if (this.editCommonModal.probability === '' || this.editCommonModal.probability === null) {
      this.nzMessageService.error('请输入中奖数值');
      return;
    }
    this.operateService.modifyLottery(
      {
        id: this.editCommonModal.id,
        name: this.editCommonModal.name,
        shortName: this.editCommonModal.shortName,
        img: this.editCommonModal.img,
        type: this.editCommonModal.type,
        groupType: this.editCommonModal.groupType,
        stock: this.editCommonModal.stock.status ? 0 : this.editCommonModal.stock.num,
        limitType: this.editCommonModal.limitIntegral.type,
        limitIntegral: this.editCommonModal.limitIntegral.type === 0 ? '' : this.editCommonModal.limitIntegral.num,
        value: this.editCommonModal.value,
        probability: this.editCommonModal.probability,
      }
    ).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.hideModal();
        this.getLottery();
      }
    });
  }

  saveEditThanksModal() {
    if (!this.editThanksModal.img) {
      this.nzMessageService.error('请选择图片');
      return;
    }
    this.operateService.modifyLottery(
      {
        id: this.editThanksModal.id,
        img: this.editThanksModal.img,
        type: 4,
        groupType: this.editThanksModal.groupType,
      }
    ).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.hideModal();
        this.getLottery();
      }
    });
  }

  stockLockChange() {
    if (this.editCommonModal.stock.status) {
      this.editCommonModal.stock.num = '';
    }
  }

  lockChange() {
    // if (this.editCommonModal.limitIntegral) {
    //   this.editCommonModal.stock.num = '';
    // }
    this.editCommonModal.limitIntegral.num = '';
  }

  move(id: any, type: any) {
    this.operateService.moveLottery(id, type).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.getLottery();
      }
    });
  }
}
